package com.cy.pet.diary.controller;

import com.cy.pet.common.service.DiaryFileService;
import com.cy.pet.common.vo.ImageVO;
import com.cy.pet.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/10/8 10:55
 */
@RestController
@RequestMapping("/file")
public class DiaryFileController {

    @Autowired
    private DiaryFileService diaryFileService;

    /**
     * 需求分析: 文件上传完成之后,需要返回ImageVO对象
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException {

        ImageVO imageVO = diaryFileService.upload(file);
        //不成功 应该返回null
        if(imageVO == null){
            return SysResult.fail();
        }
        return SysResult.success(imageVO);
    }

    /**
     * 需求: 实现图片删除
     * URL: http://localhost:8091/file/deleteFile?virtualPath=xxx
     * 参数: virtualPath= /2021/11/11/xxx.jpg
     * 返回值结果: SysResult
     */
    @DeleteMapping("/deleteFile")
    public SysResult deleteFile(String virtualPath){

        diaryFileService.deleteFile(virtualPath);
        return SysResult.success();
    }
}
